﻿using System.Collections;
using UnityEngine;
#if UNITY_EDITOR || UNITY_ANDROID || UNITY_IOS
using CSUtils;
public class U3dSingleton<T> : MonoBehaviour where T : MonoBehaviour
{
    protected static T _instance;

    public static T Instance
    {
        get
        {
            if (_instance == null)
            {
                _instance = (T)Object.FindObjectOfType(typeof(T));
                if (_instance == null)
                {
                    var go = GameObject.CreatePrimitive(PrimitiveType.Cube);
                    go.name = typeof(T).ToString();
                    _instance = go.AddComponent<T>();
                    _instance.enabled = false;
                    _instance.enabled = true;
                }
                GameObject.DontDestroyOnLoad(_instance.gameObject);
            }
            return _instance;
        }
    }
    public static T GetObj => Instance;

    public void Destroy()
    {
        if (this != null && _instance != null)
        {
            _SubDestroy();
            // Destroy(_instance.gameObject);
            _instance = null;
        }
    }

    protected virtual void _SubDestroy()
    {
    }
}
#endif
